<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<!-- 3、请实现find函数，使下列的代码调用正确。
约定：
 title数据类型为String
userId为主键，数据类型为Number
var data = [
  {userId: 8, title: 'title1'},
  {userId: 11, title: 'other'},
  {userId: 15, title: null},
  {userId: 19, title: 'title2'}
];

var find = function(origin) {
  //请补充你的代码
}
//查找data中，符合条件的数据，并进行排序
var result = find(data).where({
  "title": /\d$/
}).orderBy('userId', 'desc');

console.log(result); // [{ userId: 19, title: 'title2'}, { userId: 8, title: 'title1' }]; -->

<body>
    <div>test12-3</div>
    <script>
        var data = [
            { userId: 8, title: 'title1' },
            { userId: 11, title: 'other' },
            { userId: 15, title: null },
            { userId: 19, title: 'title2' }
        ];
        var find = function (origin) {
            return {
                data: origin,
                where: function (obj) {
                    for (let key in obj) {
                        console.log(obj, key)
                        if (obj.hasOwnProperty(key)) {
                            this.data = this.data.filter(v => obj[key].test(v[key]))
                        }
                        // return find(this.data) // return在此处写，where只能过滤一个筛选条件
                    }
                    return find(this.data)
                },
                orderBy: function (key, order) {
                    this.data.sort((a, b) => {
                        if (order == 'desc') {
                            return b[key] - a[key]
                        } else {
                            return a[key] - b[key]
                        }
                    })
                    return this.data
                }
            }
        }

        var result = find(data).where({
            "title": /\d$/
        }).orderBy('userId', 'desc');

        console.log(result); // [{ userId: 19, title: 'title2'}, { userId: 8, title: 'title1' }];

    </script>
</body>

</html>